﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;

namespace KUMC.Neurology.SomatoTopo
{
    class SubjectTable : DataTable
    {
        /*
         * Constructor
         * */
        public SubjectTable()
        {
        }
        public SubjectTable(string[] columns)
        {
            try
            {
                foreach (string col in columns)
                {
                    this.Columns.Add(col);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        /*
         * Methods
         * */
        public void UpdateFromStudy(Study study)
        {
            if (study != null)
            {
                try
                {
                    if (this.Columns.Count == 0)
                    {
                        foreach (string col in study.SubjectOption)
                        {
                            this.Columns.Add(col);
                        }
                    }

                    this.Rows.Clear();
                    study.Export2DataTable(this);

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }        
        public void Export2Excel(string savefile)
        {
            Excel.Application xls;
            Excel._Workbook wbook;
            Excel._Worksheet wSheet;

            try
            {
                xls = new Excel.Application();

                wbook = (Excel._Workbook)(xls.Workbooks.Add(Missing.Value));
                wSheet = (Excel._Worksheet)wbook.ActiveSheet;

                // Header
                int c = 1;
                int r = 1;
                foreach (DataColumn col in this.Columns)
                {
                    wSheet.Cells[r, c++] = col.ColumnName;
                }

                // Data
                r = 2; c = 1;
                foreach (DataRow row in this.Rows)
                {
                    foreach (object col in row.ItemArray)
                    {
                        wSheet.Cells[r, c++] = col.ToString();
                    }
                    r++;
                    c = 1;
                }

                xls.Visible = false;
                xls.UserControl = false;

                wbook.SaveAs(savefile, Excel.XlFileFormat.xlWorkbookNormal,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing, Type.Missing);
                wbook.Close(false, Type.Missing, Type.Missing);
                
                xls.Quit();

            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
    }
}
